﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace Agiltastic
{
    public partial class fDog : FormBase
    {
         private DogDao _DogDao = new DogDao();
        private ClubDao _clubDao = new ClubDao();
        private HandlerDao _handlerDao = new HandlerDao();
        private BreedDao _breedDao = new BreedDao();
        private List<Dog> _listDog = new List<Dog>();
        private Dog _currentSelected;

        public fDog()
        {
            InitializeComponent();
            try
            {
                LoadGrid();
                LoadClub();
                LoadBreed();
                LoadHandler();
                LoadDegree();
                LoadCategory();
                EnableControls(false);
                CurrentMode = Mode.DoNothing;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

        private void LoadCategory()
        {
            cmbCategory.DataSource = _DogDao.GetAllCategory();
        }
        private void LoadDegree()
        {
            cmbDegree.DataSource = _DogDao.GetAllDegree();
        }
        private void LoadHandler()
        {
            cmbHandler.DataSource = _handlerDao.GetAllHandler();
        }
        private void LoadBreed()
        {
            cmbBreed.DataSource = _breedDao.GetAllBreed();
        }

        private void LoadGrid()
        {
            _listDog = _DogDao.GetAllDog();
            dgvDog.DataSource = _listDog;
        }


        private void LoadClub()
        {
            cmbBreed.DataSource = _clubDao.GetAllClub();
        }

        private void EnableControls(bool b)
        {
            txtName.Enabled = b;
            cmbCategory.Enabled = b;
            cmbDegree.Enabled = b;
            cmbHandler.Enabled = b;
            cmbBreed.Enabled = b;
            btnSave.Enabled = b;
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            dgvDog.DataSource = _listDog.FindAll(c => c.Name.ToUpperInvariant().Contains(txtFilter.Text.ToUpperInvariant()));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            txtName.Text = "";
            _currentSelected = new Dog();
            CurrentMode = Mode.Add;
            EnableControls(true);
        }


        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvDog.SelectedCells.Count > 0)
            {
                var name = (String)dgvDog.SelectedCells[1].Value;
                if (name != "")
                {
                    _currentSelected = _listDog.Find(c => c.Name == name);

                    txtName.Text = _currentSelected.Name;
                    cmbBreed.SelectedIndex = cmbBreed.FindStringExact(_currentSelected.Breed1.Name);
                    cmbCategory.SelectedIndex = cmbCategory.FindStringExact(_currentSelected.Category1.Name);
                    cmbDegree.SelectedIndex = cmbDegree.FindStringExact(_currentSelected.Degree1.Name);
                    cmbHandler.SelectedIndex = cmbHandler.FindStringExact(_currentSelected.Handler1.Name);
                    CurrentMode = Mode.Edit;
                    EnableControls(true);
                }
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtName.Text))
                MessageBox.Show("The first name cannot be empty");
            else
            {
                _currentSelected.Name = txtName.Text;
                _currentSelected.Handler = ((Handler)cmbHandler.SelectedItem).Id;
                _currentSelected.HandlerName = ((Handler)cmbHandler.SelectedItem).Name;

                _currentSelected.Breed = ((Breed)cmbBreed.SelectedItem).Id;
                _currentSelected.BreedName = ((Breed)cmbBreed.SelectedItem).Name;

                _currentSelected.Category = ((Category)cmbCategory.SelectedItem).Id;
                _currentSelected.CategoryName = ((Category)cmbCategory.SelectedItem).Name;

                _currentSelected.Degree = ((Degree)cmbDegree.SelectedItem).Id;
                _currentSelected.DegreeName = ((Degree)cmbDegree.SelectedItem).Name;
                if (CurrentMode == Mode.Edit)
                {
                    _DogDao.Update(_currentSelected);
                }

                if (CurrentMode == Mode.Add)
                {
                    _DogDao.Save(_currentSelected);
                }
                LoadGrid();
                EnableControls(false);
            }
        }
    }
}
